﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace DLL.Entities
{
    public static class Utility
    {
        public static bool IsFileOpen(string file)
        {
            int tryCount = 0;

            while (tryCount < 1)
            {
                using (FileStream stream = new FileStream(file, FileMode.OpenOrCreate, FileAccess.Read, FileShare.None))
                    try
                    {
                        stream.ReadByte();
                        return false;
                    }
                    catch (IOException)
                    {
                        return true;//catch exceptions as per Joe
                    }
                    finally
                    {
                        stream.Close();
                        stream.Dispose();
                    }
            }
            return false;
        }

        public static bool DeleteFile(string fileToDelete)
        {
            try
            {
                if (File.Exists(fileToDelete))
                {
                    File.Delete(fileToDelete);
                    return true;
                }
                else
                    return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
